package com.yihu.wlyy.service.quota;

import com.yihu.wlyy.entity.SignFamily;
import com.yihu.wlyy.entity.WlyyJobConfig;
import com.yihu.wlyy.entity.WlyyQuota;
import com.yihu.wlyy.entity.doctor.DoctorPatientGroupInfo;
import com.yihu.wlyy.job.QuartzHelper;
import com.yihu.wlyy.repository.*;
import com.yihu.wlyy.web.quota.WlyyJobConfigVO;
import com.yihu.wlyy.web.quota.WlyyQuotaVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by Administrator on 2016/8/16.
 */
@Service
public class JobService {
    @Autowired
    private QuartzHelper quartzHelper;
    @Autowired
    private WlyyJobConfigDao wlyyJobConfigDao;
    @Autowired
    private QuotaDao quotaDao;

    @Autowired
    private SignFamilyDao signFamilyDao;
    @Autowired
    private DoctorPatientGroupInfoDao doctorPatientGroupInfoDao;

    @Transactional
    public void stopById(String id) throws Exception {
        WlyyJobConfig wlyyJobConfig = wlyyJobConfigDao.findById(id, "1");
        if (wlyyJobConfig != null) {
            quartzHelper.removeJob(wlyyJobConfig.getId());
            wlyyJobConfig.setStatus("0");
        } else {
            throw new Exception("任务已经停止");
        }
    }

    @Transactional
    public void startById(String id) throws Exception {
        WlyyJobConfig wlyyJobConfig = wlyyJobConfigDao.findById(id, "0");
        if (wlyyJobConfig != null) {
            startOneJob(wlyyJobConfig);
        } else {
            throw new Exception("任务已经启动");
        }
    }


    @Transactional
    public void stopAll() throws Exception {
        List<WlyyJobConfig> wlyyJobConfigs = wlyyJobConfigDao.findByAll("1");
        if (wlyyJobConfigs != null && wlyyJobConfigs.size() > 0) {
            for (WlyyJobConfig wlyyJobConfig : wlyyJobConfigs) {
                quartzHelper.removeJob(wlyyJobConfig.getId());
                wlyyJobConfig.setStatus("0");
            }
        } else {
            throw new Exception("任务已经全部停止");
        }
    }

    @Transactional
    public void startAll() throws Exception {
        List<WlyyJobConfig> wlyyJobConfigs = wlyyJobConfigDao.findByAll("0");
        if (wlyyJobConfigs != null && wlyyJobConfigs.size() > 0) {
            for (WlyyJobConfig wlyyJobConfig : wlyyJobConfigs) {
                startOneJob(wlyyJobConfig);
            }
        } else {
            throw new Exception("任务已经全部启动");
        }
    }

    /**
     * 启动单个任务
     *
     * @param wlyyJobConfig
     * @throws Exception
     */
    private void startOneJob(WlyyJobConfig wlyyJobConfig) throws Exception {
        WlyyQuota wlyyQuota = quotaDao.findOne(wlyyJobConfig.getQuotaId());

        WlyyQuotaVO wlyyQuotaVO = new WlyyQuotaVO();
        WlyyJobConfigVO wlyyJobConfigVO = new WlyyJobConfigVO();
        BeanUtils.copyProperties(wlyyQuota, wlyyQuotaVO);
        BeanUtils.copyProperties(wlyyJobConfig, wlyyJobConfigVO);

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("quota", wlyyQuotaVO);
        params.put("jobConfig", wlyyJobConfigVO);
        if (!StringUtils.isEmpty(wlyyJobConfig.getJobClass())) {
            //往quartz框架添加任务
            quartzHelper.addJob(Class.forName(wlyyJobConfig.getJobClass()), wlyyJobConfig.getQuartzCron(), wlyyJobConfig.getId(), params);
            wlyyJobConfig.setStatus("1");//设置任务状态是启动
        }
    }

    public void startNowById(String id) throws Exception {
        WlyyJobConfig wlyyJobConfig = wlyyJobConfigDao.findOne(id);
        WlyyQuota wlyyQuota = quotaDao.findOne(wlyyJobConfig.getQuotaId());

        WlyyQuotaVO wlyyQuotaVO = new WlyyQuotaVO();
        WlyyJobConfigVO wlyyJobConfigVO = new WlyyJobConfigVO();
        BeanUtils.copyProperties(wlyyQuota, wlyyQuotaVO);
        BeanUtils.copyProperties(wlyyJobConfig, wlyyJobConfigVO);

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("quota", wlyyQuotaVO);
        params.put("jobConfig", wlyyJobConfigVO);
        //往quartz框架添加任务

        if (!StringUtils.isEmpty(wlyyJobConfig.getJobClass())) {
            quartzHelper.startNow(Class.forName(wlyyJobConfig.getJobClass()), wlyyJobConfig.getId(), params);
        }
    }


    public void productDataByDay(Integer day) throws Exception {
        List<WlyyJobConfig> wlyyJobConfigs = wlyyJobConfigDao.findByIds();
        for (WlyyJobConfig wlyyJobConfig : wlyyJobConfigs) {
            WlyyQuota wlyyQuota = quotaDao.findOne(wlyyJobConfig.getQuotaId());

            WlyyQuotaVO wlyyQuotaVO = new WlyyQuotaVO();
            WlyyJobConfigVO wlyyJobConfigVO = new WlyyJobConfigVO();
            BeanUtils.copyProperties(wlyyQuota, wlyyQuotaVO);
            BeanUtils.copyProperties(wlyyJobConfig, wlyyJobConfigVO);

            Map<String, Object> params = new HashMap<String, Object>();
            params.put("quota", wlyyQuotaVO);
            params.put("jobConfig", wlyyJobConfigVO);
            for (int i = 1; i <= day; i++) {
                //往quartz框架添加任务
                params.put("now", getYesterday(0 - i + 1));
                params.put("yesterday", getYesterday(0 - i));
                if (!StringUtils.isEmpty(wlyyJobConfig.getJobClass())) {
                    quartzHelper.startNow(Class.forName(wlyyJobConfig.getJobClass()), wlyyJobConfig.getId() + UUID.randomUUID(), params);
                    Thread.sleep(15000L);
                }
            }
        }
    }

    public static String getYesterday(Integer day) {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, day);
        String yesterday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
        return yesterday;
    }

    public void productDataByOneDay(String yesterday) throws Exception {
        SimpleDateFormat dataSimple = new SimpleDateFormat("yyyy-MM-dd");

        Date date = dataSimple.parse(yesterday);
        if (date == null) {
            throw new Exception("时间格式错误");
        }
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.DATE, 1);//把日期往后增加一天.整数往后推,负数往前移动
        Date nowDate = calendar.getTime();   //这个时间就是日期往后推一天的结果
        String now = new SimpleDateFormat("yyyy-MM-dd").format(nowDate.getTime());
        List<WlyyJobConfig> wlyyJobConfigs = wlyyJobConfigDao.findByIds();
        for (WlyyJobConfig wlyyJobConfig : wlyyJobConfigs) {
            WlyyQuota wlyyQuota = quotaDao.findOne(wlyyJobConfig.getQuotaId());

            WlyyQuotaVO wlyyQuotaVO = new WlyyQuotaVO();
            WlyyJobConfigVO wlyyJobConfigVO = new WlyyJobConfigVO();
            BeanUtils.copyProperties(wlyyQuota, wlyyQuotaVO);
            BeanUtils.copyProperties(wlyyJobConfig, wlyyJobConfigVO);

            Map<String, Object> params = new HashMap<String, Object>();
            params.put("quota", wlyyQuotaVO);
            params.put("jobConfig", wlyyJobConfigVO);
            //往quartz框架添加任务
            params.put("now", now);
            params.put("yesterday", yesterday);
            if (!StringUtils.isEmpty(wlyyJobConfig.getJobClass())) {
                quartzHelper.startNow(Class.forName(wlyyJobConfig.getJobClass()), wlyyJobConfig.getId() + UUID.randomUUID(), params);
                Thread.sleep(15000L);
            }

        }
    }

    public void productDataByOneDayWithId(String yesterday, String id) throws Exception {

        SimpleDateFormat dataSimple = new SimpleDateFormat("yyyy-MM-dd");
        Date date = dataSimple.parse(yesterday);
        if (date == null) {
            throw new Exception("时间格式错误");
        }
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.DATE, 1);//把日期往后增加一天.整数往后推,负数往前移动
        Date nowDate = calendar.getTime();   //这个时间就是日期往后推一天的结果
        String now = new SimpleDateFormat("yyyy-MM-dd").format(nowDate.getTime());

        WlyyJobConfig wlyyJobConfig = wlyyJobConfigDao.findById(id);
        if (wlyyJobConfig == null) {
            throw new Exception("id不存在");
        }
        WlyyQuota wlyyQuota = quotaDao.findOne(wlyyJobConfig.getQuotaId());
        WlyyQuotaVO wlyyQuotaVO = new WlyyQuotaVO();
        WlyyJobConfigVO wlyyJobConfigVO = new WlyyJobConfigVO();
        BeanUtils.copyProperties(wlyyQuota, wlyyQuotaVO);
        BeanUtils.copyProperties(wlyyJobConfig, wlyyJobConfigVO);

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("quota", wlyyQuotaVO);
        params.put("jobConfig", wlyyJobConfigVO);
        //往quartz框架添加任务
        params.put("now", now);
        params.put("yesterday", yesterday);
        if (!StringUtils.isEmpty(wlyyJobConfig.getJobClass())) {
            quartzHelper.startNow(Class.forName(wlyyJobConfig.getJobClass()), wlyyJobConfig.getId() + UUID.randomUUID(), params);
            Thread.sleep(15000L);
        }


    }

    @Transactional
    public void startaaaa() {
        List<SignFamily> s = signFamilyDao.findByDate("2016-08-05 15:40:00");
        for (SignFamily ss : s) {
            List<DoctorPatientGroupInfo> DoctorPatientGroupInfos=doctorPatientGroupInfoDao.findByPatient(ss.getPatient());
            if(DoctorPatientGroupInfos!=null&&DoctorPatientGroupInfos.size()==0){
                DoctorPatientGroupInfo qkDoctorPatientGroupInfo=new DoctorPatientGroupInfo();
                qkDoctorPatientGroupInfo.setCzrq(new Date());
                qkDoctorPatientGroupInfo.setQyrq(ss.getApplyDate());
                qkDoctorPatientGroupInfo.setGroup("2");
                qkDoctorPatientGroupInfo.setPatient(ss.getPatient());
                qkDoctorPatientGroupInfo.setPartAmount(0);
                qkDoctorPatientGroupInfo.setStatus(ss.getStatus()>0?1:0);
                if(ss.getStatus()<0){
                    qkDoctorPatientGroupInfo.setDqrq(ss.getApplyUnsignDate());
                }
                qkDoctorPatientGroupInfo.setSignType("1");
                qkDoctorPatientGroupInfo.setDoctor(ss.getDoctor());
                doctorPatientGroupInfoDao.save(qkDoctorPatientGroupInfo);

                qkDoctorPatientGroupInfo=new DoctorPatientGroupInfo();
                qkDoctorPatientGroupInfo.setCzrq(new Date());
                qkDoctorPatientGroupInfo.setQyrq(ss.getApplyDate());
                qkDoctorPatientGroupInfo.setGroup("2");
                qkDoctorPatientGroupInfo.setPatient(ss.getPatient());
                qkDoctorPatientGroupInfo.setPartAmount(0);
                qkDoctorPatientGroupInfo.setStatus(ss.getStatus()>0?1:0);
                if(ss.getStatus()<0){
                    qkDoctorPatientGroupInfo.setDqrq(ss.getApplyUnsignDate());
                }
                qkDoctorPatientGroupInfo.setSignType("1");
                qkDoctorPatientGroupInfo.setDoctor(ss.getDoctorHealth());
                doctorPatientGroupInfoDao.save(qkDoctorPatientGroupInfo);
            }
        }
        s = signFamilyDao.findByDate("2016-08-16 00:00:00");
        for (SignFamily ss : s) {
            List<DoctorPatientGroupInfo> DoctorPatientGroupInfos=doctorPatientGroupInfoDao.findByPatient(ss.getPatient());
            if(DoctorPatientGroupInfos!=null&&DoctorPatientGroupInfos.size()==0){
                DoctorPatientGroupInfo qkDoctorPatientGroupInfo=new DoctorPatientGroupInfo();
                qkDoctorPatientGroupInfo.setCzrq(new Date());
                qkDoctorPatientGroupInfo.setQyrq(ss.getApplyDate());
                qkDoctorPatientGroupInfo.setGroup("2");
                qkDoctorPatientGroupInfo.setPatient(ss.getPatient());
                qkDoctorPatientGroupInfo.setPartAmount(0);
                qkDoctorPatientGroupInfo.setStatus(ss.getStatus()>0?1:0);
                if(ss.getStatus()<0){
                    qkDoctorPatientGroupInfo.setDqrq(ss.getApplyUnsignDate());
                }
                qkDoctorPatientGroupInfo.setSignType("1");
                qkDoctorPatientGroupInfo.setDoctor(ss.getDoctor());
                doctorPatientGroupInfoDao.save(qkDoctorPatientGroupInfo);

                qkDoctorPatientGroupInfo=new DoctorPatientGroupInfo();
                qkDoctorPatientGroupInfo.setCzrq(new Date());
                qkDoctorPatientGroupInfo.setQyrq(ss.getApplyDate());
                qkDoctorPatientGroupInfo.setGroup("2");
                qkDoctorPatientGroupInfo.setPatient(ss.getPatient());
                qkDoctorPatientGroupInfo.setPartAmount(0);
                qkDoctorPatientGroupInfo.setStatus(ss.getStatus()>0?1:0);
                if(ss.getStatus()<0){
                    qkDoctorPatientGroupInfo.setDqrq(ss.getApplyUnsignDate());
                }
                qkDoctorPatientGroupInfo.setSignType("1");
                qkDoctorPatientGroupInfo.setDoctor(ss.getDoctorHealth());
                doctorPatientGroupInfoDao.save(qkDoctorPatientGroupInfo);
            }
        }
    }
}
